package com.offcn.com.offcn.strings;

import java.util.HashMap;
import java.util.Scanner;

/*
获取字符串中最长回文串
 */
public class LongestTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = sc.nextLine();

        getLongest(str);
        System.out.println(isPalindrome(str));

    }

    public static void getLongest(String str) {
        //创建HashMap对象
        HashMap<String, Integer> hm = new HashMap<>();
        //获取字符串的所有子串
        for (int i = 0; i < str.length() ; i++) {
            for (int j = i+1; j <= str.length() ; j++) {
                String subStr = str.substring(i, j);
                int len = subStr.length();
                if(isPalindrome(subStr)) {
                    hm.put(subStr, len);
                }
            }
        }
        System.out.println(hm);
    }


    //判断一个字符串是否是回文串
    private static boolean isPalindrome(String str) {
        if(str == null || str.length() == 0) {
            return false;
        }
        for (int i = 0; i <str.length(); i++) {
            if(str.charAt(i) != str.charAt(str.length() -1 - i)) {
                return false;
            }
        }
        return true;
    }

}
